home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / netzwerk / tcp-ip / ipdial_v1.9 / vsscanf.c < prev    next >
C/C++ Source or Header  |  1996-02-26  |  5KB  |  204 lines

  1. /**
  2. ***  IPDial     Script program for initializing a SLIP connection
  3. ***  Copyright  (C)   1994    Jochen Wiedmann
  4. ***
  5. ***  This program is free software; you can redistribute it and/or modify
  6. ***  it under the terms of the GNU General Public License as published by
  7. ***  the Free Software Foundation; either version 2 of the License, or
  8. ***  (at your option) any later version.
  9. ***
  10. ***  This program is distributed in the hope that it will be useful,
  11. ***  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ***  GNU General Public License for more details.
  14. ***
  15. ***  You should have received a copy of the GNU General Public License
  16. ***  along with this program; if not, write to the Free Software
  17. ***  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ***
  19. ***
  20. ***
  21. ***  This file implements a function very similar to sscanf().
  22. ***
  23. ***
  24. ***  Computer: Amiga 1200                       Compiler: Dice 3.01
  25. ***
  26. ***  Author:    Jochen Wiedmann
  27. ***             Am Eisteich 9
  28. ***             72555 Metzingen
  29. ***             Germany
  30. ***
  31. ***             Phone: (+0049) 7123 / 14881
  32. ***             Internet: wiedmann@neckar-alb.de
  33. **/
  34.  
  35.  
  36.  
  37.  
  38.  
  39. /**
  40. ***  Include files
  41. **/
  42. #ifndef IPDIAL_H
  43. #include "IPDial.h"
  44. #endif
  45. #include <ctype.h>
  46.  
  47.  
  48.  
  49.  
  50. /**
  51. ***  Scan the character buf for a string like %} and terminate it
  52. ***  with a '\0'. Return the pointer after %}
  53. **/
  54. STRPTR ReadWord(STRPTR buf, UBYTE c)
  55.  
  56. { while(*buf)
  57.   { if (*buf == '%')
  58.     { char *ptr;
  59.  
  60.       if (buf[1] == c)
  61.       { *buf = '\0';
  62.     return(buf+2);
  63.       }
  64.  
  65.       ptr = (char*) buf;
  66.       while (ptr[1])
  67.       { *ptr = ptr[1];
  68.     ++ptr;
  69.       }
  70.       *ptr = '\0';
  71.     }
  72.     else
  73.     { ++buf;
  74.     }
  75.   }
  76.   return(buf);
  77. }
  78.  
  79.  
  80. /**
  81. ***  This is a function very similar to sscanf: A buffer is scanned
  82. ***  according to a format string.
  83. ***
  84. ***  The format string is interpreted as follows:
  85. ***
  86. ***     - The sequence %{WORD%} means any number of characters until the
  87. ***       word WORD is found. (Use %% to include the character '%'
  88. ***       into WORD itself.
  89. ***     - A blank means any number (including 0) and kind (blank, tab,
  90. ***       return, line feed, form feed) of white space characters.
  91. ***     - The sequence %[VAR%]%(SUFFIX%) means to store the following
  92. ***       white space separated word into the environment variable VAR.
  93. ***       The optional SUFFIX will be removed, if it is present.
  94. ***
  95. ***  Other characters are simply ignored.
  96. ***
  97. ***  The function returns the number of environment variables created.
  98. **/
  99. LONG Vsscanf(STRPTR buffer, STRPTR format, ULONG flags)
  100.  
  101. { char *buf, *fmt;
  102.   int result;
  103.  
  104.   /*  Copy strings, so that we can modify them. */
  105.   if (!(buf = strdup((char*) buffer))  ||
  106.       !(fmt = strdup((char*) format)))
  107.   { perror("malloc");
  108.     exit(10);
  109.   }
  110.   buffer = (STRPTR) buf;
  111.   format = (STRPTR) fmt;
  112.  
  113.   result = 0;
  114.   while (*fmt  &&  *buf)
  115.   { if (isspace(*fmt))
  116.     { while (isspace(*buf))
  117.       { ++buf;
  118.       }
  119.       ++fmt;
  120.     }
  121.     else if (*fmt == '%')
  122.     { ++fmt;
  123.       if (*fmt == '{')
  124.       { char *word;
  125.     int len;
  126.  
  127.     /* Read word to match    */
  128.     word = fmt+1;
  129.     fmt = (char*) ReadWord((STRPTR) word, '}');
  130.     len = strlen(word);
  131.  
  132.     /*  Word read, look into buffer  */
  133.     if (!len)
  134.     { continue;
  135.     }
  136.  
  137.     while (*buf  &&  strncmp(buf, word, len) != 0)
  138.     { ++buf;
  139.     }
  140.     if (*buf)
  141.     { buf += len;
  142.     }
  143.       }
  144.       else if (*fmt == '[')
  145.       { char *var, *val, *suffix;
  146.  
  147.     /*  Read variable name.     */
  148.     var = fmt+1;
  149.     fmt = (char*) ReadWord((STRPTR) var, ']');
  150.  
  151.     /*  Read suffix             */
  152.     if (strncmp(fmt, "%(", 2) == 0)
  153.     { suffix = fmt+2;
  154.       fmt = (char*) ReadWord((STRPTR) suffix, ')');
  155.     }
  156.     else
  157.     { suffix = "";
  158.     }
  159.  
  160.     /*  Read next word from buffer  */
  161.     val = buf;
  162.     while (*buf  &&  !isspace(*buf))
  163.     { ++buf;
  164.     }
  165.     if (*buf)
  166.     { *buf = '\0';
  167.       ++buf;
  168.     }
  169.  
  170.     /*  Check for suffix            */
  171.     if (strlen(suffix) < strlen(val)  &&
  172.         strcmp(val + strlen(val) - strlen(suffix), suffix) == 0)
  173.     { val[strlen(val) - strlen(suffix)] = '\0';
  174.     }
  175.  
  176.     /*  Set environment variable    */
  177.     if (*var)
  178.     { if (VerboseMode)
  179.       { fprintf(stderr, "Setting environment variable %s to value %s.\n",
  180.             var, val);
  181.       }
  182.       if (!setvar((STRPTR) var, (STRPTR) val, flags))
  183.       { fprintf(stderr, "Error while setting environment variable %s to value %s.\n",
  184.             var, val);
  185.       }
  186.       ++result;
  187.     }
  188.       }
  189.       else
  190.       { /*  Ignore this character.    */
  191.     ++fmt;
  192.       }
  193.     }
  194.     else
  195.     { ++fmt;     /*  Ignore this character   */
  196.     } 
  197.   }
  198.  
  199.   free(buffer);
  200.   free(format);
  201.  
  202.   return(result);
  203. }
  204.